package com.whity.towerdefense.model;

import java.util.ArrayList;

import android.graphics.PointF;
import android.util.Log;

import com.whity.towerdefense.view.GraphicElt;
import com.whity.towerdefense.view.GraphicHeart;

public class Shoot extends GraphicElt implements I_Tickable
{
	// =============================================================
	// Private attributs :
	
	private final static String		TAG = "com.whity.towerdefense.model";
	
	// General attributs
	@SuppressWarnings("unused")
	private final String 			m_TypeName;
	
	private float					m_Range;
	private float					m_Damage;
	private float					m_SplashRadius;
	private float					m_SplashReduction;
	private int						m_Bounce;
	private float					m_BounceReduction;
	private float					m_Speed;
	private I_Targetable			m_Target;
	private PointF					m_Pos;
	
	// =============================================================
	// Protected functions :
	
	public Shoot(String p_TypeName, float p_Size, float p_Range, float p_Damage, float p_SplashRadius, float p_SplashReduction, int p_Bounce, float p_BounceReduction, GraphicHeart p_GraphicHeart, float p_X, float p_Y, I_Targetable p_Target)
	{
		super(p_GraphicHeart);
		m_TypeName			= p_TypeName;
		m_Speed				= 3.0f;
		m_Range				= p_Range;
		m_Damage			= p_Damage;
		m_SplashRadius		= p_SplashRadius;
		m_SplashReduction	= p_SplashReduction;
		m_Bounce			= p_Bounce;
		m_BounceReduction	= p_BounceReduction;
		m_Target			= p_Target;
		
		
		m_Pos 				= new PointF(p_X, p_Y);
		SetScale(p_Size/(GetWidth()/Map.UNIT_SIZE));
		SetPosition(m_Pos.x * Map.UNIT_SIZE - GetWidth()*GetScaleX()/2, m_Pos.y * Map.UNIT_SIZE - GetHeight()*GetScaleY()/2);
	}
	
	// =============================================================
	// Public functions

	public boolean GameTick(int p_Ms)
	{
		float walkDistance = (m_Speed/1000.0f) * p_Ms;
		
		PointF dest = m_Target.GetPosition();
		PointF direction = new PointF(dest.x-m_Pos.x, dest.y-m_Pos.y);
		float dest_distance = (float) Math.sqrt(direction.x*direction.x+direction.y*direction.y);
		
		if(dest_distance < 0.05)
		{
			m_Target.OnHit(this);
			GameModel.LEVEL.RemoveShoot(this);
			return false;
		}
		else
		{
			float ratio;
			if(dest_distance < walkDistance)
			{
				walkDistance = dest_distance;
				ratio = 1.0f;
			}
			else
			{
				ratio = (float)(walkDistance/dest_distance);
			}
			
			PointF deplacement = new PointF(direction.x*ratio, direction.y*ratio);
			m_Pos.x += deplacement.x;
			m_Pos.y += deplacement.y;
			SetPosition(m_Pos.x * Map.UNIT_SIZE - GetWidth()*GetScaleX()/2, m_Pos.y * Map.UNIT_SIZE - GetHeight()*GetScaleY()/2);
			return true;
		}
		
	}
	
	public float GetDamage()
	{
		return m_Damage;
	}
	
	// =============================================================
}